/**
 * Created by webhugo on 16-10-16.
 */
var db = require('./index.js');
var co = require('co');

var util = require('util');
var utilx = require('../lib/utilx');
var config = require('../config/config');
var url =  require('url');


function *addUser() {
    for (var i = 0; i < 100; i++) {
        var user = {
            username:'name'+i,
            password:'123',
            description : "一些介绍",
            type: i % 2,
            avatar: '/default_avatar.jpg',
            email : `554565808${i}@qq.com`,
            sex : i % 2,
            city: "大连",
            sign : "个性签名"
        };
        yield db.models.User.create(user);
    }
}


// function *addTopic() {
//     for (var i = 1; i < 100; i++) {
//         var topic = {
//             title: 'title' + i,
//             text: 'text' + i,
//             replyCount: 0,
//             readCount: 0,
//             UserId: i
//         }
//         yield db.models.Topic.create(topic);
//     }
// }


function *addComment() {
    for (var i = 1; i < 100; i++) {
        var comment = {
            text: 'comment' + i,
            type: i%2 +1,
            star: 100
        };
        yield db.models.Comment.create(comment);
    }

}


function * fieldSeed() {
    //yield db.models.Field.bulkCreate([
    //    "CSS", "NodeJs", "iOS", "Android", "Linux", "计算机网络", "操作系统", "数据库", "NoSQL",
    //    "React Native", "C#", "PHP", "Python", "Java", "JS", "HTML", "Angular"
    //].map(field => ({
    //    Caption: field,
    //    Count: 10
    //})));
    var list = [
        "CSS", "NodeJs", "iOS", "Android", "Linux", "计算机网络", "操作系统", "数据库", "NoSQL",
        "React Native", "C#", "PHP", "Python", "Java", "JS", "HTML", "Angular"
    ];
    for(var i in list){
        yield db.models.Field.create({
            title:list[i],
            count:10
        })
    }

}

var majors = ["嵌入式","Web","Android","IOS","UI"]

function * courseSeed() {
    var fields = ["Web", "Android", "IOS", "UI"];
    var eng2chs = ["一", "二", "三", "四", "五"]
    var users = yield db.models.User.findAll({
        where: {
            type: 1
        }
    });

    var courses = [];
    fields.forEach(field => {

    });

    var tags = ["入门", "由浅入深", "中级", "高级"];
    for (let j = 0; j < eng2chs.length; j++) {
        for (let i = 0; i < fields.length; i++) {
            let field = fields[i];
            //     let tag = tags[j];
            let course = yield db.models.Course.create({
                description: `${field}组第${eng2chs[j]}次沙龙`,
                title: `${field}组第${eng2chs[j]}次沙龙`,
                participantCount: utilx.randomNum(3),
                difficulty: i % 3 + 1,
                cover: "/default_cover.jpg",
                starNum: utilx.randomNum(3),
                followNum: utilx.randomNum(3),
                teacher: i + "teacher",
                score: utilx.randomNum(1),
                chapter: utilx.randomNum(1),
                location: url.resolve("http://" + config.server.ip + ":" + config.server.port ,"/static/default_video.mp4"),
                major : majors[ i % majors.length ]
            });

            let fieldOfCourse = yield db.models.FieldsOfCourse.create({
                CourseId: course.id,
                FieldId: field.id
            });
        }
    }
}

function * ChapterSeed() {
    var courses = yield db.models.Course.findAll();

    var chapters = [];
    courses.forEach(course => {
        ['入门', '深入'].forEach((tag, index) => {
            chapters.push({
                title: tag,
                num: 10,
                CourseId: course.id
            })
        })
    });

    yield db.models.Chapter.bulkCreate(chapters);
}


function * LessonSeed() {
    var chapters = yield db.models.Chapter.findAll();

    var Lesson = [];
    chapters.forEach(chapter => {
        ['视频教学'].forEach((tag, index) => {
            Lesson.push({
                title: tag,
                level: parseInt(utilx.randomNum(1)) % 2 + 1,
                timeLength: 100,
                ChapterId: chapter.id
            })
        })
    });

    yield db.models.Lesson.bulkCreate(Lesson);
}

// function * noteSeed() {
//     var courses = yield db.models.Course.findAll();
//     var users = yield db.models.User.findAll();
//
//     var notes = [];
//     var i = 0;
//     courses.forEach(course => {
//         [
//             '收获很大',
//             '点赞'
//         ].forEach((tag, index) => {
//             notes.push({
//                 Content: tag,
//                 UserId: users[i ++ % users.length ].id,
//                 CourseId: course.id
//             })
//         })
//     });
//
//     yield db.models.Note.bulkCreate(notes);
// }

function * postsSeed(){
    var users = yield db.models.User.findAll();
    for(var i=0;i<30;i++){
        yield db.models.Post.create({
            title : `帖子${i}`,
            text : `<html>这里是帖子的内容</html>`,
            replyCount : 10,
            readCount : 100,
            UserId : users[i].id
        })
    }
}

function * postReplySeed(){
    var posts = yield db.models.Post.findAll();
    var users = yield db.models.User.findAll();
    for(var i=0;i<100;i++){
        yield db.models.Reply.create({
            text : "这里是回复的内容",
            type : i % 2 + 1,
            PostId : posts[i % 30].id,
            UserId : users[i % 100].id
        })
    }
}

function* init() {
    // yield db.sync({
    //     force: true
    // });

    // yield fieldSeed();
    // yield addUser();
    // yield courseSeed();
    // yield ChapterSeed();
    // yield LessonSeed();
    // yield noteSeed();
    // yield addTopic();
    // yield addComment();
    yield postsSeed();
    yield postReplySeed();
}

co(function*() {
    // yield db.sync({});
    yield init();
    // yield db.models.Carousel.sync({
    //     force : true
    // })
    // yield db.models.File.sync({
    //     force : true
    // })
    // yield db.models.Comment.sync({
    //     force : true
    // })
    // yield db.models.User.sync({
    //     force : true
    // })
    // yield db.models.History.sync({
    //     force : true
    // })
    // yield db.models.Post.sync({
    //     force : true
    // })
    console.log('finished ...');
}).catch(function () {
    console.log(arguments);
});
